package com.codejiwei.utils

import com.alibaba.fastjson.JSON
import com.codejiwei.sample.Book
import org.json4s.jackson.JsonMethods
import org.json4s.{Extraction, JValue}
import org.json4s.jackson.JsonMethods.compact

/**
 * Author: jiwei01
 * Date: 2022/7/15 00:31
 * Package: com.codejiwei.utils
 * Description:
 */
object JsonUtils {
  implicit val format = org.json4s.DefaultFormats

  def toAny[T: Manifest](s: String): T = JsonMethods.parse(s).extract[T]

  def parseToJValue(s: String): JValue = JsonMethods.parse(s)

  def toJson(o: Any): String = compact(toJValue(o))

  def toJValue(o: Any): JValue = Extraction.decompose(o)


  def main(args: Array[String]): Unit = {
    val str = "{\"id\":1, \"title\":\"history\", \"authors\":\"Tom\", \"year\": 1996}"
    val book = JsonUtils.toAny[Book](str)
//    println(book)

    val value: Book = JSON.parseObject(str, classOf[Book])
    println(value)




  }

}
